package dataBase;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.PhoneNumber;
import com.google.appengine.api.datastore.Text;

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
public class Order {
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;
	@Persistent
	private int orderNum;
	@Persistent
	private Date startTime;
	@Persistent
	private String startTimeFormat;
	@Persistent
	private Date endTime;
	@Persistent
	private String endTimeFormat;
	@Persistent
	private Text comments;
	@Persistent
	private Text messages;
	@Persistent
	private Text notesClient;
	@Persistent
	private String destAdd;
	@Persistent
	private PhoneNumber phoneNumber;
	@Persistent
	private Boolean isActive;
	@Persistent
	private Boolean statusNotification;
	@Persistent
	private Boolean messageNotification;
	@Persistent
	private List<Key> statusList = new ArrayList<Key>();
	@Persistent
	private List<String> statusNamesList;
	@Persistent
	private List<Key> levelsList = new ArrayList<Key>();
	@Persistent
	private String businessUserName;
	@Persistent
	private Boolean messageToRead; 
	@Persistent
	private String email; //biz add
	@Persistent
	private Boolean sendMail; //for the email that the biz add
	@Persistent
	private String details;
	@Persistent
	private List<Key> clientList = new ArrayList<Key>();
	@Persistent
	private Integer vote;
	@Persistent
	private Integer urgency;
	

	
	public int getVote() {
		if(vote==null)
			return 0;
		return vote;
	}
	public void setVote(int vote) {
		this.vote = vote;
	}
	public List<Key> getClientList() {
		return clientList;
	}
	public void setClientList(List<Key> clientList) {
		this.clientList = clientList;
	}
	public Key getKey() {
		return key;
	}
	public int getOrderNum() {
		return orderNum;
	}
	public Date getStartTime() {
		return startTime;
	}
	public Text getComments() {
		return comments;
	}
	public Text getNotesClient() {
		return notesClient;
	}
	public String getDestAdd() {
		return destAdd;
	}
	public PhoneNumber getPhoneNumber() {
		return phoneNumber;
	}
	public Boolean getIsActive() {
		return isActive;
	}
	public void setKey(Key key) {
		this.key = key;
	}
	public void setOrderNum(int orderNum) {
		this.orderNum = orderNum;
	}
	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}
	public void setComments(Text notesBiz) {
		this.comments = notesBiz;
	}
	public void setNotesClient(Text notesClient) {
		this.notesClient = notesClient;
	}
	public void setDestAdd(String destAdd) {
		this.destAdd = destAdd;
	}
	public void setPhoneNumber(PhoneNumber phoneNumber) {
		this.phoneNumber = phoneNumber;
	}
	public void setIsActive(Boolean isActive) {
		this.isActive = isActive;
	}
	
	public void removeFromLevelsList(Level level) {
		this.levelsList.remove(level);
	}
	public void addToLevelsList(Level level) {
		this.levelsList.add(level.getKey());
	}
	
	public List<Key> getLevelsList() {
		return levelsList;
	}
	public void setLevelsList(List<Key> levelsList) {
		this.levelsList = levelsList;
	}

	public void addMessage(Text newMessage) {

		//System.out.println("###########addMessage############");
		//System.out.println("new: "+newMessage.getValue());
		//if (messages!=null)
			//System.out.println("message: "+messages.getValue());
		
		if(messages==null)
			messages = newMessage;
		else
			messages=new Text(messages.getValue()+"\n"+newMessage.getValue());
		
		//System.out.println("###########addMessage - end############");
		
	}
	
	public Text getMessages() {
		if(messages==null)
			return new Text("");
		return messages;
	}
	
	public void setMessages(Text messages) {
		this.messages = messages;
	}
	public String getStartTimeFormat() {
		return startTimeFormat;
	}
	public void setStartTimeFormat(String startTimeFormat) {
		this.startTimeFormat = startTimeFormat;
	}
	public Boolean getStatusNotification() {
		return statusNotification;
	}
	public void setStatusNotification(Boolean statusNotification) {
		this.statusNotification = statusNotification;
	}
	public Boolean getMessageNotification() {
		return messageNotification;
	}
	public void setMessageNotification(Boolean messageNotification) {
		this.messageNotification = messageNotification;
	}
	public String getBusinessUserName() {
		return businessUserName;
	}
	public void setBusinessUserName(String businessUserName) {
		this.businessUserName = businessUserName;
	}
	public Boolean getMessageToRead() {
		return messageToRead;
	}
	public void setMessageToRead(Boolean messageToRead) {
		this.messageToRead = messageToRead;
	}
	public String getEmail() {
		if (email==null)
			return "";
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public Boolean getSendMail() {
		return sendMail;
	}
	public void setSendMail(Boolean sendMail) {
		this.sendMail = sendMail;
	}
	public String getDetails() {
		if (details==null)
			return "";
		
		return details;
	}
	public void setDetails(String details) {
		this.details = details;
	}
	public List<Key> getStatusList() {
		return statusList;
	}
	public void setStatusList(List<Key> statusList) {
		this.statusList = statusList;
	}
	
	public List<String> getStatusNamesList() {
		return statusNamesList;
	}
	
	public void setStatusNamesList(List<String> statusNamesList) {
		this.statusNamesList = statusNamesList;
	}
	public void addClient(Key key) {
		if (!clientList.contains(key))
			clientList.add(key);
		System.out.println("add key: "+key);
		
	}
	public void setEndTime(Date date) {
		endTime=date;
		
	}
	public Date getEndTime() {
		return endTime;
		
	}
	public void setEndTimeFormat(String dateString) {
		endTimeFormat = dateString;
		
	}
	public String getEndTimeFormat() {
		return endTimeFormat;
		
	}
	
	public boolean isDelay(Long[] urgencyTime,Date currentDate) {
		
		//valid parameters
		if (urgencyTime==null || getUrgency()>= urgencyTime.length || urgencyTime[getUrgency()]==null || startTime==null ||urgencyTime[getUrgency()]==0)
			return false;
			
		if (currentDate==null)
			currentDate = new Date();
		
		if (currentDate.getTime() - startTime.getTime() > urgencyTime[getUrgency()])
			return true;
		
		return false;
	}
	public Integer getUrgency() {
		if(urgency==null || urgency<0 )
			return 1;
		return urgency;
	}
	public void setUrgency(Integer urgency) {
		this.urgency = urgency;
	}
	public Date getDelayDate(Long[] urgencyTime){
		if(urgencyTime[getUrgency()]==0)
			return null;
		return new Date(this.startTime.getTime()+urgencyTime[getUrgency()]); 
	}

}
